package com.blockmeta.bbs.minemodule.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.blankj.utilcode.util.SpanUtils;
import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.minemodule.R;
import com.blockmeta.bbs.minemodule.pojo.MyPostsPOJO;
import com.chad.library.adapter.base.BaseViewHolder;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by author on 2018/4/19.
 */

public class MyPostsAdapter extends BaseAdapter<MyPostsPOJO.ListBean,BaseViewHolder> {
    private Context mContext;
    private boolean editable = false;
    private boolean chooseSingle = false;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    private int mSelectedPos = -1;

    public MyPostsAdapter(int layoutResId, @Nullable List<MyPostsPOJO.ListBean> data, Context context) {
        super(layoutResId, data);
        mContext = context;
    }


    @Override
    protected void convert(BaseViewHolder helper, final MyPostsPOJO.ListBean item) {
        final String type = item.getType() == null ? "0" : item.getType();
        TextView title = helper.getView(R.id.bbs_item_title);

        String attachment = item.getAttachment() == null ? "" : item.getAttachment();
        //TODO 服务器未传输此参数
        String digest = item.getDigest();
        digest = "0";
        switch (type) {
            case "0": //普通
                if (attachment.equals("2")) {
                    title.setText(new SpanUtils()
                                          .append(item.getTitle() + " ")
                                          .appendImage(mContext.getResources().getDrawable(R.drawable.comment_list_pic), SpanUtils.ALIGN_CENTER)
                                          .create());

                } else {
                    title.setText(item.getTitle());
                }
                break;
            case "1"://投票  绿色
                initTag(item, title, digest, attachment,mContext.getResources().getDrawable(R.drawable.vote_lable));
                break;
            case "2"://商品  黑色
                initTag(item, title, digest, attachment,mContext.getResources().getDrawable(R.drawable.product_lable));
                break;
            case "3"://悬赏   橘黄
                initTag(item, title, digest, attachment,mContext.getResources().getDrawable(R.drawable.award_lable));
                break;
            case "4"://活动  红色
                initTag(item, title, digest, attachment,mContext.getResources().getDrawable(R.drawable.activity_lable));
                break;
            case "5"://辩论  //蓝色
                initTag(item, title, digest, attachment,mContext.getResources().getDrawable(R.drawable.debet_lable));
                break;
            default:
//                helper.setText(R.mPostName.bbs_item_title, item.getTitle());
                title.setText(item.getTitle());
                break;
        }
        TextView tv_content = helper.getView(R.id.bbs_item_content);
        tv_content.setText(item.getContent());


        ToggleButton toggleButton = helper.getView(R.id.tg_edit_selector);
        toggleButton.setVisibility(editable? View.VISIBLE:View.GONE);
        toggleButton.setTag(helper.getAdapterPosition());
        toggleButton.setOnCheckedChangeListener(null);
        toggleButton.setChecked(isItemChecked(helper.getAdapterPosition()));
        toggleButton.setOnCheckedChangeListener(mCheckedListener);
        ImageView view = helper.getView(R.id.bbs_item_icon);
//        Glide.with(mContext).load(item.getAuthor_info().getAvatar()).placeholder(R.drawable.yoona).into(view);
        Picasso.with(mContext).load(item.getAuthor_info().getAvatar()).placeholder(R.drawable.yoona).into(view);
        //转换时间
        String longToString = FormatUtil.StringDataToString(item.getPost_date() + "");
        helper.setText(R.id.bbs_item_author, item.getAuthor_info().getName() + "/" + longToString);
        helper.setText(R.id.bbs_item_count, item.getReplies() + "");
        LinearLayout view1 = helper.getView(R.id.item_bbs_main);
        view1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (type.equals("4")) {
                    //活动
                    RouterHelper.startActivity(mContext, RouterPath.ACTIVE_DETAIL_ACTIVITY,item.getTid()+"");
                } else if (type.equals("3")) {
                    //悬赏
                    RouterHelper.buildPath(RouterPath.BBS_REWARD_ACTIVITY)
                            .withString("tid",item.getTid()+"")
                            .navigation(mContext);
                } else if (type.equals("1")) {
                    //投票
                    RouterHelper.buildPath(RouterPath.BBS_VOTE_ACTIVITY)
                            .withString("tid",item.getTid()+"")
                            .navigation(mContext);
                } else {
                    RouterHelper.buildPath(RouterPath.BBS_DETAIL_ACTIVITY)
                            .withString("tid",item.getTid()+"")
                            .navigation(mContext);
                }
            }
        });
    }
    private void initTag(MyPostsPOJO.ListBean item, TextView title, String digest, String attachment, Drawable resId) {
        if (attachment.equals("2")) {
            if (!digest.equals("0")) {
                title.setText(new SpanUtils()
                                      .appendImage(mContext.getResources().getDrawable(R.drawable.essence_lable), SpanUtils.ALIGN_CENTER)
                                      .appendImage(resId, SpanUtils.ALIGN_CENTER)
                                      .append(" " + item.getTitle() + " ")
                                      .appendImage(mContext.getResources().getDrawable(R.drawable.comment_list_pic), SpanUtils.ALIGN_CENTER)
                                      .create());

            } else {
                title.setText(new SpanUtils()
                                      .appendImage(resId, SpanUtils.ALIGN_CENTER)
                                      .append(" " + item.getTitle() + " ")
                                      .appendImage(mContext.getResources().getDrawable(R.drawable.comment_list_pic), SpanUtils.ALIGN_CENTER)
                                      .create());
            }
        } else {
            if (!digest.equals("0")) {
                title.setText(new SpanUtils()
                                      .appendImage(mContext.getResources().getDrawable(R.drawable.essence_lable), SpanUtils.ALIGN_CENTER)
                                      .appendImage(resId, SpanUtils.ALIGN_CENTER)
                                      .append(" " + item.getTitle() + " ")
                                      .create());

            } else {
                title.setText(new SpanUtils()
                                      .appendImage(resId, SpanUtils.ALIGN_CENTER)
                                      .append(" " + item.getTitle() + " ")
                                      .create());
            }
        }
    }

    private CheckedListener mCheckedListener = new CheckedListener();

    class CheckedListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if(chooseSingle){
                int position = (int) buttonView.getTag();
                if (mSelectedPos == position) {
                    setItemChecked(position, false);
                    mSelectedPos = -1;
                } else if (mSelectedPos != position && mSelectedPos != -1) {
                    //先取消上个item的勾选状态
                    setItemChecked(mSelectedPos, false);
                    notifyItemChanged(mSelectedPos);
                    //设置新Item的勾选状态
                    mSelectedPos = position;
                    setItemChecked(mSelectedPos, true);
                    notifyItemChanged(position);
                } else if (mSelectedPos == -1) {
                    mSelectedPos = position;
                    setItemChecked(mSelectedPos, true);
                    notifyItemChanged(position);
                }
            }else {
                int position = (int) buttonView.getTag();
                setItemChecked(position, isChecked);
            }
        }
    }

    public void setEditable(boolean editable, boolean chooseSingle) {
        this.editable = editable;
        this.chooseSingle = chooseSingle;
        mSelectedPos = -1;
        notifyDataSetChanged();
    }

    public List<MyPostsPOJO.ListBean> getCheckedItems() {
        ArrayList<MyPostsPOJO.ListBean> selectList = new ArrayList<>();
        int size = mSelectedPositions.size();
        for (int i = 0; i < size; i++) {
            if(mSelectedPositions.valueAt(i)){
                selectList.add(getItem(mSelectedPositions.keyAt(i)));
            }
        }
        return selectList;
    }

    public int getSelectedPos() {
        return mSelectedPos;
    }


    //设置给定位置条目的选择状态
    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }
}
